package com.huike.controller.adminController;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huike.entity.Rotation;
import com.huike.service.IRotationService;
import com.huike.utils.FileUploadUtil;
import com.huike.utils.JsonUtils;
import com.huike.utils.ResultMessage;
import com.huike.utils.UUIDUtil;

/**
 * 首页轮播图管理控制器
 */
@Controller
@RequestMapping("/admin/rotationManagerController")
public class RotationManagerController {
	
	@Autowired
	IRotationService rotationService;
	
    @RequestMapping("")
    public String page(){
        return "admin/rotationManager";
    }
    
    /**
     * 展示rotation列表
     * @param page 当前页
     * @param limit 当前显示数据条数
     * @return
     */
    @RequestMapping("/listRotation")
    @ResponseBody
    public void listRotation(HttpServletResponse response,int page,int limit){
        IPage<Rotation> res = rotationService.listRotationByPage(page,limit);
        Map<String,Object> map = new HashMap<>();
        JSONObject json = new JSONObject();
        map.put("code",0);
        map.put("total",res.getTotal());
        map.put("data",res.getRecords());
        json.put("code",0);
        json.put("total",res.getTotal());
        json.put("data",res.getRecords());
        JsonUtils.jsonArray(map,response);
    }
    
    /**
     * 轮播图管理的添加方法
     * @param rotation
     * @return
     */
    @RequestMapping("/addRotationFunc")
    @ResponseBody
    public ResultMessage addRotationFunc(Rotation rotation){
    	System.out.println("add test1111111111111111111111111");
        Boolean b = rotationService.save(rotation);
        if (b){
            return ResultMessage.success();
        }
        return ResultMessage.fail();
    }
    
    /**
     * 轮播图管理的修改方法
     * @param rotation
     * @return
     */
    @RequestMapping("/updateRotationFunc")
    @ResponseBody
    public ResultMessage updateRotationFunc(Rotation rotation){
        Boolean b =  rotationService.updateById(rotation);
        if (b){
            return ResultMessage.success();
        }
        return ResultMessage.fail();
    }
    
    /**
     * 轮播图管理的删除方法
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteRotationFunc")
    public ResultMessage deleteRotationFunc(@RequestParam(required = false,value = "ids[]") List<String> ids){
        Boolean b = rotationService.removeByIds(ids);
        if (b){
            return ResultMessage.success();
        }
        return ResultMessage.fail();
    }
    
    /*
     * 上传轮播图图片方法
     * */
     @ResponseBody
     @RequestMapping("/uploadimg")
     public Map<String,Object> uploadimg(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

         String oldName = file.getOriginalFilename();
         InputStream inputStream = file.getInputStream();
         String newName = UUIDUtil.getUUId() + oldName.substring(oldName.lastIndexOf("."));
         System.out.println("imgurl test:" + newName);
         Map map = new HashMap<String,Object>();
         //文件路径

         try {
             String resUrl = FileUploadUtil.upload(newName,inputStream);

             if (resUrl==null){
                 return null;
             }
             //返回json
             map.put("msg","ok");
             map.put("code",200);
             map.put("src",resUrl);

         } catch (Exception e) {
             map.put("msg","error");
             map.put("code",0);
             e.printStackTrace();
         }
         return map;
     }
    
}
